home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / unsoft11.zip / UNSOFT11.C < prev    next >
Text File  |  1984-06-09  |  6KB  |  245 lines

  1. /************************************************************************/
  2. /*         Program to convert Wordstar Document-Mode        */
  3. /*        files to plain-vanilla, non-document, ascii format.        */
  4. /*                                    */
  5. /*    by Paul Homchick, One Radnor Station #300, Radnor, PA 19087    */
  6. /*                                    */
  7. /*      will compile with Software Toolworks C/80, or CI C86,        */
  8. /*            or Digital Research C                */
  9. /*                                                 */
  10. /*                  last edited: 15:33, 6/09/84                */
  11. /************************************************************************/
  12.  
  13. #include    <stdio.h>
  14. #define VERSION    "1.1C"
  15. #define VDATE    "09 Jun 84"
  16. #define CR    0x0d
  17. #define LF    0x0a
  18. #define CPM_EOF 0x1a        /* cp/m end of file mark        */
  19. #define FF    0x0c        /* form feed ( )            */
  20. #define TAB    0x09
  21. #define CNTRL_O 0x0f        /* non-break space            */    
  22. #define END_HYP 0x1f        /* soft hyphen at end of line        */
  23. #define ERROR    -1
  24. #define TRUE    1
  25. #define FALSE    0
  26. #define BIGLINE    512        /* number of char in biggest line    */
  27.  
  28. #define DRC            /* define this to compile with Dig Res C*/
  29.                 /* comment out if not DRC        */
  30.  
  31. static int inptr, outptr, startline;
  32.  
  33. main(argc,argv)
  34. int argc; char *argv[];
  35. {
  36.     register int c;
  37.  
  38.     startline= TRUE;
  39.     switch (argc)
  40.     {
  41.         case 1:
  42.             usage();
  43.             exit(0);
  44.         case 2:
  45.             if ( (!strcmp(argv[1],"?")) ||
  46.                  (cmdeq(argv[1],"[HELP]")) ||
  47.                  (cmdeq(argv[1],"[help]")) )
  48.                 help();
  49.             else
  50.             {
  51.                 usage();
  52.                 error("Not enough arguments on command line.");
  53.                 exit(0);
  54.             }
  55.         case 3:
  56.             break;
  57.         default:
  58.             usage();
  59.             error("Too many arguments on command line.");
  60.             exit(0);
  61.     }
  62.  
  63.     if (strcmp(argv[1],argv[2])==0)
  64.     {
  65.         printf("Input and output filenames must differ.");
  66.         printf("  Aborting...\007\n");
  67.         exit(0);
  68.     }
  69.  
  70. #ifdef DRC
  71.     if (!(inptr= fopenb(argv[1],"r")))
  72. #else
  73.     if (!(inptr= fopen(argv[1],"rb")))
  74. #endif
  75.     {
  76.         printf("Can't open '%s' for input.\n",argv[1]);
  77.         exit(0);
  78.     }
  79.  
  80. #ifdef DRC
  81.     if (!(outptr=fopenb(argv[2],"w")))
  82. #else
  83.     if (!(outptr=fopen(argv[2],"wb")))
  84. #endif
  85.     {
  86.         printf("Can't open '%s' for output.",argv[2]);
  87.         printf("  The disk directory is probably full.\n");
  88.         exit(0);
  89.     }
  90.  
  91. /************************************************************************/
  92. /*                 main loop                */
  93. /************************************************************************/
  94.  
  95.     printf("processing... ");
  96.     while ((c=getc(inptr))!=ERROR && c!=CPM_EOF)
  97.         putc(translate(c),outptr);
  98.     putc(CPM_EOF,outptr);
  99.     fclose(inptr);
  100.     fclose(outptr);
  101.     printf("done.\n");
  102. }
  103.  
  104. /************************************************************************/
  105. /*            wordstar translation routine            */
  106. /************************************************************************/
  107.  
  108. translate(c)
  109. register int c;
  110. {
  111. char    buf[BIGLINE];
  112.  
  113.     c&= 0x7f;            /* strip high bit        */
  114.     if (startline)
  115.     {
  116.         switch (c)
  117.         {
  118.             case '.':    /* process dot commands        */
  119.                 fgets(buf,BIGLINE,inptr);
  120.                     /* .pa to form feed        */
  121.                 if ((cmdeq(buf,"PA")) || (cmdeq(buf,"pa")) )
  122.                     putc(FF,outptr);
  123.                 startline= TRUE;
  124.                 return(translate(c= getc(inptr)));
  125.             case LF:
  126.                 return(c);
  127.             default:
  128.                 startline= FALSE;
  129.         }
  130.     }
  131.     if (c < ' ')            /* check for control character    */
  132.     {
  133.         switch (c)
  134.         {
  135.             case END_HYP:
  136.                 return('-');
  137.             case CNTRL_O:
  138.                 return(' ');
  139.             case LF:
  140.             case FF:
  141.             case TAB:
  142.                 return(c);
  143.             case CR:
  144.                 startline= TRUE;
  145.                 return(c);
  146.             case CPM_EOF:
  147.                 return(CPM_EOF);
  148.             default:
  149.                 c= getc(inptr);
  150.                 return(translate(c));
  151.         }    
  152.     }
  153.     else
  154.         return(c);
  155. }
  156.  
  157. /************************************************************************/
  158. /*            short usage prompt routine            */
  159. /************************************************************************/
  160.  
  161. usage()
  162. {
  163.     printf("unsoft version %s %s\n\n",VERSION,VDATE);
  164.     printf("usage: unsoft wordstar_input_name ascii_output_name\n");
  165.     printf("   or: unsoft (? | [help]) for help.\n");
  166.     printf("converts wordstar document-mode files ");
  167.     printf("to plain text format.\n");
  168. }
  169.  
  170. /************************************************************************/
  171. /*            error print routine                */
  172. /************************************************************************/
  173.  
  174. error(s)
  175. char *s;
  176. {
  177.     printf("\007Error: %s\n",s);
  178. }
  179.  
  180. /************************************************************************/
  181. /*            on-line program help routine            */
  182. /************************************************************************/
  183.  
  184. help()
  185. {
  186.     printf("\nUnsoft is a program to filter files prepared under Wordstar\n");
  187.     printf("document mode.  Given a Wordstar document mode file as an\n");
  188.     printf("input, unsoft will output a file having made the following\n");
  189.     printf("transformations:\n\n");
  190.     printf("\to High bits stripped from all characters.\n");
  191.     printf("\to Converts '.pa' dot commands to form feed (^L).\n");
  192.     printf("\to Removes all other dot command lines from file.\n");
  193.     printf("\to Converts 'non-break-space' (^O) to a space.\n");
  194.     printf("\to Converts soft hyphen at end of line (1F hex) to '-'.\n");
  195.     printf("\to Passes through CR, LF, FF, and TAB characters.\n");
  196.     printf("\to Filters out all other control characters.\n");
  197.     printf("\nUsage: unsoft wordstar_input_name");
  198.     printf(" ascii_output_name\n");
  199.     printf("where (names) are input and output file names.  Unsoft ?,\n");
  200.     printf("or [help] will evoke this help message.\n");
  201.     exit(0);
  202. }
  203.  
  204. /************************************************************************/
  205. /*        get a line from the input file                */
  206. /*        (modified from CI C86 library)                */
  207. /************************************************************************/
  208.  
  209. fgets(line,maxline,fd)
  210. char *line;
  211. int maxline;
  212. unsigned int fd;
  213. {
  214.     int j,cc;
  215.  
  216.     for (j= 0;j < maxline-1;)
  217.     {
  218.           if((cc= (getc(inptr)&0x7F)) == CPM_EOF)
  219.             return(CPM_EOF);
  220.         line[j]= cc;
  221.         if (cc<1)
  222.             break;
  223.         if (line[j++]=='\n')
  224.             break;
  225.     }
  226.     line[j]=0;
  227.     if (j)
  228.         return(line);
  229.     else
  230.         return(0);
  231. }
  232.  
  233. /************************************************************************/
  234. /*            test input commands                */
  235. /************************************************************************/
  236.  
  237. cmdeq(s,p)
  238. char *s, *p;
  239. {
  240.     while(*p)
  241.         if(*s++ != *p++)
  242.             return(FALSE);
  243.     return(TRUE);
  244. }
  245.